//
//  WellcomeViewController.swift
//  weibo 666
//
//  Created by mac on 15/11/9.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class WellcomeViewController: UIViewController {
    
    override func loadView() {
        view = backImageView
        setupUI()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        ///异步加载用户头像
//        iconImageView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        
        iconImageView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)
    }
    ///改变视图的动画和约束
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        ///更改约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height+200)
        }
        ///设置动画
        wellcomLabel.alpha = 0
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 修改所有`可动画`属性 - 自动布局的动画
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.wellcomLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        print("OK")
                        // 不推荐的写法
                        // UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                        // 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
                })
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    private lazy var iconImageView: UIImageView = {
        let iv = UIImageView(imageName: "avatar_default_big")
        
        //设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    //欢迎标签
    private lazy var wellcomLabel: UILabel = UILabel(title: "wellcom 大神", fontSize: 20, color: UIColor.redColor())
    
    
}
///设置界面
extension WellcomeViewController{
    
    private func setupUI(){
    
        //1添加控件
        view.addSubview(iconImageView)
        view.addSubview(wellcomLabel)
        
        //2设置自动布局
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        
        }
        wellcomLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            
            
        }
        
    }
    
    
}

